﻿
namespace SwtorCalculator {
    using System;
    using System.Collections.Generic;
    using System.Threading;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Media.Imaging;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        /* Large background image location */
        private string bgImagePrefix = @"pack://application:,,,/SwtorCalculator;component/Images/";
        /* Class icon location */
        private string classIconPrefix = @"pack://application:,,,/SwtorCalculator;component/Images/ClassIcons/";

        internal static Dictionary<string, Dictionary<string, int>> items = new Dictionary<string, Dictionary<string, int>>(); // Item name(key) => [Stat(key) => Amount(value)](value) 

        ToolTip tt = new ToolTip();

        private bool IsCalcExpanded { get; set; }

        /// <summary>
        /// MainWindow no-arg constructor
        /// </summary>
        public MainWindow() {
            try {
                InitializeComponent();
                IsCalcExpanded = false;
                setClassInfo();
                setTalentTrees();
                itemHead.SlotName = "Head";
                itemEar.SlotName = "Ear";
                itemChest.SlotName = "Chest";
                itemWrist.SlotName = "Wrist";
                itemHands.SlotName = "Hand";
                itemWaist.SlotName = "Waist";
                itemLegs.SlotName = "Leg";
                itemFeet.SlotName = "Foot";
                itemImplant.SlotName = "Implant";
                itemRelicOne.SlotName = "RelicOne";
                itemRelicTwo.SlotName = "RelicTwo";
                itemRelicThree.SlotName = "RelicThree";
                itemWeapMH.SlotName = "WeaponMH";
                itemWeapOH.SlotName = "WeaponOH";
            }
            catch( Exception e ) {
                General.ErrorMessage( e.Source.ToString(), e.Message, e.TargetSite.ToString(), e.StackTrace );
            }
        }

        private void Window_MouseLeftButtonDown( object sender, MouseButtonEventArgs e ) {
            try {
                this.DragMove();
            }
            // This type of error gets thrown everytime the 'Go Back' button is hit and then you return to the main window
                // I'll fix it eventually
            catch( InvalidOperationException ex ) { }
        }

        private void btnMinimize_Click( object sender, RoutedEventArgs e ) {
            this.WindowState = WindowState.Minimized;
        }

        private void btnClose_Click( object sender, RoutedEventArgs e ) {
            this.Close();
        }

        private void MouseHover( object sender, MouseEventArgs e ) {
            Label lbl = e.Source as Label;
            lbl.Foreground = SystemColors.MenuHighlightBrush;
        }

        private void MouseExit( object sender, MouseEventArgs e ) {
            Label lbl = e.Source as Label;
            lbl.Foreground = SystemColors.WindowTextBrush;
        }

        private void lblThanks_Click( object sender, MouseButtonEventArgs e ) {
            var winThank = new winThanks();
            winThank.Owner = this;
            winThank.Show();
        }

        private void ExpandLabel_MouseLeftButtonDown( object sender, MouseButtonEventArgs e ) {
            var sb = new Storyboard();
            var da = new DoubleAnimation();
            var ta = new ThicknessAnimation();

            if( !IsCalcExpanded ) {
                CalcPane.Visibility = Visibility.Visible;
                da.From = 0;
                da.To = 624.0;
                ta.From = new Thickness( 571, 310, 0, 0 );
                ta.To = new Thickness( 1177, 310, 0, 0 );
                ExpandLabel.ToolTip = "Collapse";
            }

            else {
                tabCalc.Visibility = Visibility.Collapsed;
                da.From = 624.0;
                da.To = 0;
                ta.From = new Thickness( 1177, 310, 0, 0 );
                ta.To = new Thickness( 571, 310, 0, 0 );
                ExpandLabel.ToolTip = "Expand";
            }

            da.Duration = new Duration( TimeSpan.FromSeconds( 0.25 ) );
            ta.Duration = new Duration( TimeSpan.FromSeconds( 0.25 ) );
            Storyboard.SetTargetName( da, CalcPane.Name );
            Storyboard.SetTargetProperty( da, new PropertyPath( Image.WidthProperty ) );
            Storyboard.SetTargetName( ta, ExpandLabel.Name );
            Storyboard.SetTargetProperty( ta, new PropertyPath( "(Margin)" ) );
            sb.Children.Add( da );
            sb.Children.Add( ta );
            sb.Completed += new EventHandler( CalcAnimation_Completed );
            sb.Begin( this );
            IsCalcExpanded = !IsCalcExpanded;
            
        }

        private void CalcAnimation_Completed( object sender, EventArgs e ) {
            if( IsCalcExpanded ) {
                // TODO : Show all the controls
                tabCalc.Visibility = Visibility.Visible;
                ExpandLabel.Background =
                    new ImageBrush( new BitmapImage( new Uri( @"pack://application:,,,/SwtorCalculator;component/Images/sidebtnCollapse.png", UriKind.Absolute ) ) );
            }
            else {
                // TODO : Collapse all the controls
                ExpandLabel.Background =
                    new ImageBrush( new BitmapImage( new Uri( @"pack://application:,,,/SwtorCalculator;component/Images/sidebtnExpand.png", UriKind.Absolute ) ) );
            }
        }

        private void lblReset_Click( object sender, MouseButtonEventArgs e ) {
            if( e.ChangedButton == MouseButton.Left ) {
                Window1 choose = new Window1();
                choose.Show();
                this.Close();
            }
        }

        private void setClassInfo() {
            try {
                setTestData();
                lblClassType.Content = string.Format( "{0} [{1}]", Character.ClassName, Character.AdvanceClass );
                playerStats.UpdateStats();
                changeFactionIcon();
                changeClassIcon();
            }
            catch( Exception e ) {
                General.ErrorMessage( e.Source, e.Message, e.TargetSite.ToString(), e.StackTrace );
            }
        }

        private void setTestData() {
            Character.Stat.Level = 26;
            Character.Stat.WillpowerGear = 495 - Character.Stat.WillpowerBase;
            Character.Stat.StrengthGear = 61 - Character.Stat.StrengthBase;
            Character.Stat.EnduranceGear = 471 - Character.Stat.EnduranceBase;
            Character.Stat.AimGear = 30 - Character.Stat.AimBase;
            Character.Stat.CunningGear = 30 - Character.Stat.CunningBase;
            Character.Stat.PresenceGear = 138 - Character.Stat.PresenceBase;
            Character.Stat.ArmorStat = 997;
            Character.Stat.AccuracyStat = 4;
            Character.Stat.CritGearStat = 23;
            Character.Stat.SurgeStat = 5;
            Character.Stat.ForcePower = 124;
            Character.Stat.Power = 48;
            Character.Stat.AccuracyStat = 4;
        }

        private void changeFactionIcon() {
            if( Character.IsRepublic )
                lblFaction.Background = new ImageBrush( new BitmapImage( new Uri( bgImagePrefix + "JediRepublicHg 1.png", UriKind.Absolute ) ) );
            else
                lblFaction.Background = new ImageBrush( new BitmapImage( new Uri( bgImagePrefix + "SithEmpireHg 1.png", UriKind.Absolute ) ) );
        }

        private void changeClassIcon() {
            if( Character.ClassName.Equals( "Sith Warrior" ) )
                lblClass.Background = new ImageBrush( new BitmapImage( new Uri( classIconPrefix + "Emp_SithWarriorSm.png", UriKind.Absolute ) ) );
            if( Character.ClassName.Equals( "Sith Inquisitor" ) )
                lblClass.Background = new ImageBrush( new BitmapImage( new Uri( classIconPrefix + "Emp_Sith InquisitorSm.png", UriKind.Absolute ) ) );
            if( Character.ClassName.Equals( "Bounty Hunter" ) )
                lblClass.Background = new ImageBrush( new BitmapImage( new Uri( classIconPrefix + "Emp_BountyHunterSm.png", UriKind.Absolute ) ) );
            if( Character.ClassName.Equals( "Imperial Agent" ) )
                lblClass.Background = new ImageBrush( new BitmapImage( new Uri( classIconPrefix + "Emp_Imperial AgentSm.png", UriKind.Absolute ) ) );
            if( Character.ClassName.Equals( "Trooper" ) )
                lblClass.Background = new ImageBrush( new BitmapImage( new Uri( classIconPrefix + "Rep_TrooperSm.png", UriKind.Absolute ) ) );
            if( Character.ClassName.Equals( "Smuggler" ) )
                lblClass.Background = new ImageBrush( new BitmapImage( new Uri( classIconPrefix + "Rep_SmugglerSm.png", UriKind.Absolute ) ) );
            if( Character.ClassName.Equals( "Jedi Knight" ) )
                lblClass.Background = new ImageBrush( new BitmapImage( new Uri( classIconPrefix + "Rep_JediKnightSm.png", UriKind.Absolute ) ) );
            if( Character.ClassName.Equals( "Jedi Consular" ) )
                lblClass.Background = new ImageBrush( new BitmapImage( new Uri( classIconPrefix + "Rep_JediConsularSm.png", UriKind.Absolute ) ) );
        }

        private void setTalentTrees() {
            // if( _character.Stat is AssassinTalents )
            if( Character.TalentSpec != null ) {
                var tal = new SwtorCalculator.Talent.TalentTab();
                tal.Margin = new Thickness( 14, -32, 0, 0 );
                tabTalents.Content = tal;
            }
        }

        private void CheckChanged( object sender, RoutedEventArgs e ) {
            playerStats.UpdateStats();
        }

        private void button1_Click( object sender, RoutedEventArgs e ) {
            //var winCalc = new CalcForm();
            //winCalc.Owner = this;
            //winCalc.Show();
            //setTalentTrees();
            //testProgressWin();
            System.Diagnostics.Debug.WriteLine( "Console : " + playerStats.accordionBody1.StatList.ActualHeight );
            System.Diagnostics.Debug.WriteLine( "Console : " + playerStats.accordionBody2.StatList.ActualHeight );
            System.Diagnostics.Debug.WriteLine( "Console : " + playerStats.accordionBody3.StatList.ActualHeight );
            System.Diagnostics.Debug.WriteLine( "Console : " + playerStats.accordionBody4.StatList.ActualHeight );
            System.Diagnostics.Debug.WriteLine( "Console : " + playerStats.accordionBody5.StatList.ActualHeight );
            System.Diagnostics.Debug.WriteLine( "Console : " + playerStats.accordionBody6.StatList.ActualHeight );
        }

        private void testProgressWin() {
            try {
                var strThread = new Thread( () => {
                    var strTest = new ProgressWindow();
                    strTest.TitleText = "Strength EP Calculation";
                    strTest.Show();
                    Thread.Sleep( 5000 );
                    strTest.Close();
                } );
                strThread.SetApartmentState( ApartmentState.STA );
                strThread.Start();

                var aimThread = new Thread( () => {
                    var aimTest = new ProgressWindow();
                    aimTest.TitleText = "Aim EP Calculation";
                    aimTest.Show();
                    Thread.Sleep( 5000 );
                    aimTest.Close();
                } );
                aimThread.SetApartmentState( ApartmentState.STA );
                aimThread.Start();

                var cunThread = new Thread( () => {
                    var cunTest = new ProgressWindow();
                    cunTest.TitleText = "Cunning EP Calculation";
                    cunTest.Show();
                    Thread.Sleep( 5000 );
                    cunTest.Close();
                } );
                cunThread.SetApartmentState( ApartmentState.STA );
                cunThread.Start();

                var wilThread = new Thread( () => {
                    var wilTest = new ProgressWindow();
                    wilTest.TitleText = "Willpower EP Calculation";
                    Thread.Sleep( 5000 );
                    wilTest.Close();
                } );
                wilThread.SetApartmentState( ApartmentState.STA );
                wilThread.Start();
            }
            catch( Exception exp ) { }
        }

        private void Window_Loaded( object sender, RoutedEventArgs e ) {
            
        }

        private void btnEpStart_Click( object sender, RoutedEventArgs e ) {
            btnEpCancel.IsEnabled = true;
            btnEpStart.IsEnabled = false;

            ProgressControl str = new ProgressControl();
            str.TitleText = "Strength EP Calculation";
            epStack.Children.Add( str );

            ProgressControl aim = new ProgressControl();
            aim.TitleText = "Aim EP Calculation";
            epStack.Children.Add( aim );

            ProgressControl cun = new ProgressControl();
            cun.TitleText = "Cunning EP Calculation";
            epStack.Children.Add( cun );

            ProgressControl wil = new ProgressControl();
            wil.TitleText = "Willpower EP Calculation";
            epStack.Children.Add( wil );

            ProgressControl acc = new ProgressControl();
            acc.TitleText = "Accuracy EP Calculation";
            epStack.Children.Add( acc );

            ProgressControl crit = new ProgressControl();
            crit.TitleText = "Crit Rating EP Calculation";
            epStack.Children.Add( crit );

            ProgressControl sur = new ProgressControl();
            sur.TitleText = "Surge EP Calculation";
            epStack.Children.Add( sur );

            ProgressControl pow = new ProgressControl();
            pow.TitleText = "Attack Power EP Calculation";
            epStack.Children.Add( pow );

            ProgressControl fp = new ProgressControl();
            fp.TitleText = "Force Power EP Calculation";
            epStack.Children.Add( fp );

            ProgressControl tp = new ProgressControl();
            tp.TitleText = "Tech Power EP Calculation";
            epStack.Children.Add( tp );
        }

        private void btnEpCancel_Click( object sender, RoutedEventArgs e ) {
            btnEpCancel.IsEnabled = false;
            btnEpStart.IsEnabled = true;
        }

        private void cmbEpType_SelectionChanged( object sender, SelectionChangedEventArgs e ) {
            if( cmbEpType.SelectedIndex != -1 )
                btnEpStart.IsEnabled = true;
        }
    }
}